package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.UserDao;
import com.example.child_protection.dto.base.BaseRequestDTO;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.UserInfoDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.CkeckUserNameRequestDTO;
import com.example.child_protection.dto.request.find.HispitalRequestDTO;
import com.example.child_protection.dto.request.find.UserPageQueryRequestDTO;
import com.example.child_protection.dto.request.find.UserQueryRequestDTO;
import com.example.child_protection.dto.response.CheckUserResponseDTO;
import com.example.child_protection.dto.response.HispitalListResponseDTO;
import com.example.child_protection.dto.response.UserPageResponseDTO;
import com.example.child_protection.dto.response.UserResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.UserModel;
import com.example.child_protection.service.UserQueryService;
import com.example.child_protection.service.UserService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserQueryServiceImpl extends MyBaseService implements UserQueryService {

    @Autowired
    public UserDao userDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public UserResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.USER_FIND_ID, requestDTO, UserResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, UserResponseDTO>() {
            @Override
            protected UserResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                UserResponseDTO responseDTO = new UserResponseDTO();
                UserModel userModel = userDao.findId(requestDTO.getId());
                UserInfoDTO user = new UserInfoDTO();
                BeanUtils.copyProperties(userModel, user);
                responseDTO.setInfo(user);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 分页 查询
     *
     * @param requestDTO
     * @return
     */
    public UserPageResponseDTO findPage(UserPageQueryRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.USER_FIND_PAGE, requestDTO, UserPageResponseDTO.class, new MyServiceExecutor<UserPageQueryRequestDTO, UserPageResponseDTO>() {
            @Override
            protected UserPageResponseDTO myprocess(UserPageQueryRequestDTO requestDTO) throws Exception {
                UserPageResponseDTO responseDTO = new UserPageResponseDTO();
                PageHelper.startPage(requestDTO.getPageNum(), requestDTO.getPageSize());
                UserModel req = new UserModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<UserModel> lists = userDao.findPage(req);
                List<UserInfoDTO> userInfos = JSONObject.parseArray(JSONArray.toJSONString(lists), UserInfoDTO.class);
                PageInfo<UserInfoDTO> pageInfo = new PageInfo<UserInfoDTO>(userInfos);
                responseDTO.setPageInfo(pageInfo);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 检测用户名是否存在
     *
     * @param requestDTO
     * @return
     */
    public CheckUserResponseDTO checkUserName(CkeckUserNameRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.USER_FIND_CHECK_USER_NAME, requestDTO, CheckUserResponseDTO.class, new MyServiceExecutor<CkeckUserNameRequestDTO, CheckUserResponseDTO>() {
            @Override
            protected CheckUserResponseDTO myprocess(CkeckUserNameRequestDTO requestDTO) throws Exception {
                CheckUserResponseDTO res = new CheckUserResponseDTO();
                UserModel req = new UserModel();
                req.setUserName(requestDTO.getUserName());
                int result = userDao.count(req);
                if (result > 0) {
                    res.setFlag(BooleanEnums.T);
                } else {
                    res.setFlag(BooleanEnums.F);
                }
                res.successfully();
                return res;
            }
        });
    }
}
